package com.easy.controller;

import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.dao.IInOrderDao;
import com.easy.service.IInOrderService;
import com.easy.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
@RestController
@RequestMapping("/inorder")
public class InOrderController {
    private static final Logger logger = LoggerFactory.getLogger(InOrderController.class);

    @Autowired
    private IInOrderService is;
    @Autowired
    private IInOrderDao dao;

    // 导出excel 入库表格
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportOrdersToExcel() {
        try {
            List<Order> orders = is.getInOrder();
            byte[] excelBytes = ExcelUtil.exportInOrderListToExcel(orders);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            // 确保文件名中没有不允许的字符，并且正确编码以避免出现下载时的乱码问题
            headers.setContentDispositionFormData("attachment", "in_orders.xlsx");

            return ResponseEntity.ok()
                    .headers(headers)
                    .body(excelBytes);

        } catch (Exception e) {
            logger.error("Error exporting orders to Excel", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @PostMapping("/add")
    public CommonResult addInOrder(@RequestBody Order order){
        int count = is.addInOrder(order);
        if(count>0) {
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }    }

    @GetMapping("/{in_id}")
        public CommonResult getOrdersByInId(@PathVariable("in_id") String in_id) {
            List<Order> orders = is.getOrdersByInId(in_id);
            return CommonResult.success(orders);
        }
        @GetMapping("/all")
        public CommonResult getIInOrder() {
            List<Order> list = is.getInOrder();
            logger.debug("Retrieved Orders: {}", list); // 打印获取到的订单数据
            return CommonResult.success(list);
        }


        @PutMapping("/edit/{id}")
        public CommonResult editInOrder(@PathVariable int id, @RequestBody Order order){
            order.setId(id);  // 将URL中的id设置到User对象中
            int count = is.editInOrder(order);
            if(count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.fail();
            }
        }

        @DeleteMapping("/del/{id}")
        public CommonResult delInOrder(@PathVariable int id) {
            int count = is.delInOrder(id);
            if (count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.fail();
            }
        }

        //  查询商品名或id
        @GetMapping("/search")
        public CommonResult searchInOrder(@RequestParam String keyword){
            List<Order> inventories = is.searchInOrder(keyword);
            return CommonResult.success(inventories);
        }

        //   分页
        @GetMapping("/paged")
        public CommonResult getPagedInOrder(@RequestParam int page, @RequestParam int size) {
            List<Order> list = is.getPagedInOrder(page, size);
            int total = is.getTotalInOrderCount();
            return CommonResult.success(list, total);
        }
}
